// pages/danger/self/selfStart/selfStart.js
var app = getApp()
var util = require('../../../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        inspectId: 0,
        inspectList: [],
        title: '',
        checkNum: '',
        checkDate: new Date(),
        checkDateStr: '',
        inspectType:0,//0表示是自查
        companyName:"",
        companyId:0,
        deptName:"",
        deptId:'',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var inspectId = options.inspectId
        var inspectType=0
        var dateStr = util.formatTime(new Date())
        this.setData({
            inspectId: inspectId,
            checkDate:new Date(),
            checkDateStr: dateStr,
            inspectType:inspectType,
        })
        this.loadInspect(inspectId)
    },

    /**
     * 加载自查检查表
     */
    loadInspect(inspectId) {
        var inspectDate = new Date()
        var header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") //读取cookie
        };

        var that = this
        wx.request({
            header: header,
            method: 'POST',
            url: app.globalData.baseUrl + '/safe/danger/inspectList/find',
            data: {
                inspectId: inspectId,
                sortField: 'inspect_project',
                sortOrder: 'asc'
            },
            success(res) {
                var inspectList = res.data.data
                for (var i = 0; i < inspectList.length; i++) {
                    inspectList[i].inspectResult = "合格"
                    inspectList[i].needChange = 0
                }
                that.setData({
                    inspectList: inspectList
                })
                var checkNum = '';
                if (that.data.inspectType == 0) {
                    checkNum = "隐患自查小程序" + util.formatTime(new Date())
                } else if (that.data.inspectType == 1) {
                    checkNum = "隐患检查小程序" + util.formatTime(new Date())
                } else {
                    checkNum = "隐患排查小程序" + util.formatTime(new Date())
                }

                //获取检查表的名字
                wx.request({
                    header: header,
                    url: app.globalData.baseUrl + '/safe/danger/inspect/find',
                    method: 'POST',
                    data: {
                        dangerInspect: {
                            id: inspectId
                        },
                    },
                    success(res) {
                        that.setData({
                            title: res.data.data.list[0].name
                        })
                        wx.request({
                            header: header,
                            url: app.globalData.baseUrl + '/safe/danger/inspectPublic/find',
                            method: 'POST',
                            data: {
                                pageCurrent: 1,
                                pageSize: 1
                            },
                            success(res) {
                                var id
                                var name
                                if (res.data.success && res.data.data.list.length > 0) {
                                    id = res.data.data.list[0].id + 1
                                    name = that.data.title + id + "-" + app.globalData.user.userId + "-"+ util.formatTime(new Date())

                                } else {
                                    name = that.data.title + "-" + app.globalData.user.userId + "-"+ util.formatTime(new Date())
                                }
                                that.setData({
                                    title: name,
                                    checkNum: name
                                })
                            }
                        })
                    }
                })

            }
        })

    },
    /**
     * 将加载的自查表发布
     */
    publicSelfInspect( ) {
        var header;
        header = {
            'Cookie': wx.getStorageSync("sessionid") //读取cookie
        };
        var params = {
            dangerInspectListPublicList: this.data.inspectList,
            checkNum: this.data.checkNum, //隐患检查编号
            inspectId: this.data.inspectId,
            leaderUser: "",
            leaderUserId: "",
            name: this.data.title,
            predictInspectDate: this.data.checkDate,
            publicCompanyId: app.globalData.user.companyId,
            publicDeptId: app.globalData.user.companyId,
            status: 2,
            inspectType:this.data.inspectType,
            targetDeptId: app.globalData.user.companyId,
            activitiParams: {
                applyUserId: app.globalData.user.userId,
                check: false,
                type: this.data.inspectType
            }
        }
        var that = this
        wx.request({
            header: header,
            url: app.globalData.baseUrl + '/safe/dangerFlow/startDangerFlow',
            method: 'POST',
            data: params,
            success(res) {
                if(res.data.success){
                    var inspectPublicId = res.data.data.businesskey
                    that.setData({
                        inspectPublicId: inspectPublicId
                    })
                    if(res.data.data.myTasks.length>0){
                        that.setData({
                            taskId: res.data.data.myTasks[0].id
                        })
                        var taskId=res.data.data.myTasks[0].id
                        wx.navigateTo({
                            url: '/pages/danger/selfAdd/index?inspectId=' + inspectPublicId + '&inspect=false&taskId=' + taskId
                            + '&inspectType=' + 0,
                        })
                    }
                }
            }
        })
    },
    /**
     * 更新时间选择的值
     * @param e
     */
    inspectListChangeData(e){
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            checkDate:new Date(e.detail.value),
            checkDateStr:e.detail.value
        })
    },

    back() {
        wx.navigateBack({
            delta: 1
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        console
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})